Skip to content

Fix document sorting in Elasticsearch shared storage#157

Open
juanpabloxk wants to merge 1 commit intomainfrom
fix-elasticsearch-sorting
Open

Fix document sorting in Elasticsearch shared storage#157
juanpabloxk wants to merge 1 commit intomainfrom
fix-elasticsearch-sorting

Conversation

@juanpabloxk
Copy link
Copy Markdown
Contributor

@juanpabloxk juanpabloxk commented Sep 30, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Adjusted result ordering to show the most recently added items first when reading data. Users will now see newest entries at the top of lists and search results.
    • Improves consistency across views that display time-ordered content; pagination and item positions may appear different due to the corrected sort order.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Sep 30, 2025

Walkthrough

Changed Elasticsearch read query sorting in read_body to order results by inserted_at in descending order.

Changes

Cohort / File(s) Summary
Elasticsearch read sorting
lib/bas/shared_storage/elasticsearch.rb
Updated read_body to sort by inserted_at descending instead of ascending.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    actor Client
    participant Storage as SharedStorage
    participant ES as Elasticsearch

    Client->>Storage: read(query)
    Storage->>ES: search(sort: inserted_at DESC)
    ES-->>Storage: results (newest first)
    Storage-->>Client: return results
    note over Storage,ES: Sorting changed from ASC to DESC on inserted_at
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I tilt my ears to hear the logs,
Newest crumbs atop the bogs—
Hop-hop through time’s descending trail,
Freshest tales now lead the tale.
In shards of search, I sniff and sort,
Latest carrots first—report! 🥕✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly and accurately describes the change to document sorting in the Elasticsearch shared storage layer, clearly highlighting the primary fix without unnecessary detail or ambiguous phrasing.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-elasticsearch-sorting

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🧪 Early access (Sonnet 4.5): enabled

We are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience.

Note:

  • Public repositories are always opted into early access features.
  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 18114611470

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 98.413%

Totals Coverage Status
Change from base Build 16480861938: 0.0%
Covered Lines: 1985
Relevant Lines: 2017

💛 - Coveralls

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 980fba2 and e53cfe7.

📒 Files selected for processing (1)
  • lib/bas/shared_storage/elasticsearch.rb (1 hunks)

}
},
sort: [{ inserted_at: { order: "asc" } }]
sort: [{ inserted_at: { order: "desc" } }]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

Verify test coverage and confirm the intended processing order.

The change from ascending to descending order means the most recently inserted document will now be processed first (LIFO) instead of the oldest (FIFO). This is a significant behavioral change that could impact consumers of this API.

Please verify:

  1. Test coverage exists for this sorting behavior
  2. No consumers depend on the previous FIFO ordering
  3. This descending order aligns with the intended business requirements

Run the following script to check for related tests and usage:


🏁 Script executed:

#!/bin/bash
# Description: Find tests and usages related to Elasticsearch shared storage ordering

# Search for test files related to Elasticsearch shared storage
echo "=== Test files for Elasticsearch shared storage ==="
fd -e rb -g '*elasticsearch*' -g '*spec*' -g '*test*'

# Search for assertions or expectations about ordering/sorting
echo -e "\n=== Test assertions about sorting or ordering ==="
rg -n -C3 --type=ruby 'inserted_at|sort|order|first|FIFO|LIFO' -g '*spec*' -g '*test*'

# Search for usages of the read method from Elasticsearch class
echo -e "\n=== Usages of SharedStorage::Elasticsearch read method ==="
rg -n -C3 --type=ruby 'SharedStorage::Elasticsearch|Elasticsearch.*\.read'

Length of output: 5424


Add tests covering descending inserted_at sorting behavior
Currently only the single-hit case is tested; add specs in spec/bas/shared_storage/elasticsearch_spec.rb verifying multiple results are returned newest-first, and confirm no downstream consumers depend on FIFO ordering or conflict with business requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants